// pages/user/mymer/detail/detail.js
const app = getApp()
const utils = require('../../../../utils/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    merchantDetail: {},
    providerList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      mid: options.mid
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let _this = this
    this.getProgress(_this.data.mid)
    
    // let _this = this
    // let random = Math.floor(Math.random() * 100)
    // if (random < 45) _this.getProgress(_this.data.mid)
    // else _this.error()
  },

  error: function () {
    wx.showLoading({
      title: '加载数据',
    })
    setTimeout(() => {
      wx.hideLoading()
      wx.showToast({
        title: '服务器无响应',
        duration: 3000
      })
    }, 5000)
  },

  getProgress: function(mid) {
    let that = this
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/v1/order-push/detail?access_token=' + accessToken,
        data: {
          id: mid
        },
        success: res => {
          if(res.data.status === 1) {
            wx.request({
              url: app.globalData.hostAPI + '/v1/order-push/sign-list?access_token=' + accessToken,
              data: {
                id: mid
              },
              success: list => {
                res.data.data.push_order_detail.service_at = utils.formatTimeThr(res.data.data.push_order_detail.service_at, 'Y-M-D')
                that.setData({
                  merchantDetail: res.data.data.push_order_detail,
                  providerList: list.data.data
                })
              }
            })
          }
        }
      })
    })
  },

  rejected: function(e) {
    let _this = this
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/v1/order-push/rejected-provider?access_token=' + accessToken,
        data: {
          id: e.currentTarget.dataset.pid,
          order_push_id: _this.data.merchantDetail.id
        },
        success: res => {
          if(res.data.status === 1) {
            _this.getProgress(_this.data.mid)
          }
        }
      })
    })
  },

  /**
   * 
   */
  over: function() {
    let _this = this
    if (_this.data.providerList.length != 0) {
      app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
        wx.request({
          url: app.globalData.hostAPI + '/v1/order-push/deadline?access_token=' + accessToken,
          data: {
            id: _this.data.merchantDetail.id
          },
          success: res => {
            if(res.data.status === 1) {
              wx.showModal({
                title: '提示',
                content: '截止报名，请注意签到保洁',
                showCancel: false, 
                success: info => {
                  wx.navigateTo({
                    url: '/pages/user/mymer/mymer',
                  })
                }
              })
            }
          }
        })
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '人数为0，不能截至',
        showCancel: false
      })
    }
  }
})